<?php

class Emms_Form_Admin_Fund extends HTech_Form
{
    protected $_primary  = 'FundId';
    
    public function __construct() 
    {
        parent::__construct();
        
        $this->setName('Fund');
        
        $this->setAttrib('title', $this->translate("Fund form"));
        $this->setDescription($this->translate("Edit fund info or add a new one"));
        
        $fundId = new Zend_Form_Element_Hidden('FundId');
        
        $code = new Zend_Form_Element_Text('Code');
        $code->setLabel("Code")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addFilter('Alnum')
            ->addFilter('StringToUpper')
            ->addValidator('StringLength',false,
                array(
                    'min'      => 2,
                    'max'      => 64,
                    'messages' => $this->translate("Enter a %max% characters value")
                ))
            ->addValidator('Db_NoRecordExists',false,
                array(
                    'table'    => 'funds', 
                    'field'    => 'Code', 
                    'exclude'  => array('field' => 'FundId', 'value' => $this->_id),
                    'messages' => $this->translate("Non valid or duplicated entry")                     
                ));
        
        $fund = new Zend_Form_Element_Text('Fund');
        $fund->setLabel("Fund")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addFilter('Alpha', array('allowwhitespace' => true))
            ->addValidator('StringLength',false,
                array(
                    'min'      => 2,
                    'max'      => 64,
                    'messages' => $this->translate("From %min% to %max% characters only")
                ))
            ->addValidator('Db_NoRecordExists',false,
                array(
                    'table'    => 'funds', 
                    'field'    => 'Fund', 
                    'exclude'  => array('field' => 'FundId', 'value' => $this->_id),
                    'messages' => $this->translate("Non valid or duplicated entry")                    
                ))
            ->setDescription(null);  
                                
        $programId = new Zend_Form_Element_Multiselect('LnkProgramsFunds');
        $programId->setLabel("Programs - press Ctrl key for multiple selection")                
            ->setAttrib('size','6')
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addMultiOptions($this->_db->fetchPairs('select ProgramId, Program from programs'));    
        
        $status = new Zend_Form_Element_Select('Status');
        $status->setLabel("Status")                
            ->setAttrib('size','2')
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addMultiOptions(array('Active' => $this->translate("Active"), 'Inactive' => $this->translate("Inactive")));   
                        
        $comments = new Zend_Form_Element_Textarea('Comments');
        $comments->setLabel("Comments")
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 4); 
        
        $submit = new Zend_Form_Element_Submit('Submit');
        $submit->setLabel("Submit")
            ->addDecorator(new HTech_Form_Decorator_Button(array('type'=>'submit')));
        
        $this->addElements(array($code,$fund,$programId,$status,$comments,$submit,$fundId));
    }
}